﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Moq;

namespace XRM.Framework.Test.Unit
{
    public class XrmUnitTest
    {
        #region Properties

        protected Mock<ITracingService> TracingServiceMock
        {
            get;
            set;
        }

        protected ITracingService TracingService
        {
            get
            {
                return TracingServiceMock.Object;
            }
        }
        
        protected Mock<IOrganizationServiceFactory> OrganizationServiceFactoryMock
        {
            get;
            set;
        }

        protected IOrganizationServiceFactory OrganizationServiceFactory
        {
            get
            {
                return OrganizationServiceFactoryMock.Object;
            }
        }

        protected Mock<IOrganizationService> SystemOrganizationServiceMock
        {
            get;
            set;
        }

        protected IOrganizationService SystemOrganizationService
        {
            get
            {
                return SystemOrganizationServiceMock.Object;
            }
        }

        #endregion

        #region Constructors

        public XrmUnitTest()
        {
            Init();
        }

        #endregion

        #region Protected Methods

        protected virtual void Init()
        {
            TracingServiceMock = new Mock<ITracingService>();
            OrganizationServiceFactoryMock = new Mock<IOrganizationServiceFactory>();
            SystemOrganizationServiceMock = new Mock<IOrganizationService>();

            OrganizationServiceFactoryMock.Setup(factory => factory.CreateOrganizationService(null)).Returns(SystemOrganizationService);
            InitOrganizationServiceMock(SystemOrganizationServiceMock);
        }

        protected void InitOrganizationServiceMock(Mock<IOrganizationService> mock)
        {
            mock.Setup(service => service.Associate(It.IsAny<string>(), It.IsAny<Guid>(), It.IsAny<Relationship>(), It.IsAny<EntityReferenceCollection>()));
            mock.Setup(service => service.Disassociate(It.IsAny<string>(), It.IsAny<Guid>(), It.IsAny<Relationship>(), It.IsAny<EntityReferenceCollection>()));
            mock.Setup(service => service.Create(It.IsAny<Entity>())).Returns(() => Guid.NewGuid());
            mock.Setup(service => service.Update(It.IsAny<Entity>()));
            mock.Setup(service => service.Delete(It.IsAny<string>(), It.IsAny<Guid>()));
        }

        #endregion
    }
}
